package com.qyl.honghengshop.controller.customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.Goods;
import com.qyl.honghengshop.service.GoodsService;
import com.qyl.honghengshop.service.impl.GoodsServiceImpl;


@Controller
@RequestMapping("/customer/goodsCar")
public class GoodsCarController {

	private GoodsService goodsService = new GoodsServiceImpl();
	
	@RequestMapping("/addGoodsCar")
	@ResponseBody
	public Map<String, String> addGoodsCar(HttpSession session,Integer buyNum,Integer goodId){
		
		//从session中获得购物车
		Map<Integer, Integer> buyCar = (Map<Integer, Integer>) session.getAttribute(PageConstant.BUYCARD);
		
		//判断从session中获得的购物车是否为空
		if(null == buyCar){
			//购物车为空，创建购物车并保存添加的商品
			buyCar = new LinkedHashMap<Integer, Integer>();
			buyCar.put(goodId, buyNum);
		}else{			
			if(buyCar.containsKey(goodId)){
				//购物车中存在该商品，把购买数量叠加
				Integer num = buyCar.get(goodId);
				buyCar.put(goodId, num+buyNum);
			}else{
				//购物车不为空，但没有该商品，保存并添加该商品到购物车
				buyCar.put(goodId, buyNum);
			}			
		}
		//把购物车保存到session中
		session.setAttribute(PageConstant.BUYCARD, buyCar);
		//保存成功，返回成功提示
		Map<String, String> map = new HashMap<String, String>();
		map.put("status", "success");		
		return map;
	}
	@RequestMapping("/getBuyNums")
	@ResponseBody
	public Map<String, Integer> getBuyNums(HttpSession session){
		
		//从session中获得购物车
		Map<Integer, Integer> buyCar = (Map<Integer, Integer>) session.getAttribute(PageConstant.BUYCARD);
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		int buyNums = 0;
		
		if(null == buyCar){
			map.put("buyNums", 0);
		}else{
			Set<Entry<Integer, Integer>> entrySet = buyCar.entrySet();
			for (Entry<Integer, Integer> entry : entrySet) {
				 buyNums += entry.getValue();
			}
			map.put("buyNums", buyNums);
		}
		return map;		
	}
	@RequestMapping("/getBuyCarList")
	public String getBuyCarList(HttpSession session,ModelMap map){
		
		//从session中的购物车
		Map<Integer,Integer> buyCar = (Map<Integer, Integer>) session.getAttribute(PageConstant.BUYCARD);
		
		//创建从购物车获得的商品的list
		List<Goods> goodsList = new ArrayList<Goods>();
		
		//总价格
		Double total = 0.0;
		if(null != buyCar){
			//遍历购物车,获得购物车中的商品id和商品的购买数量
			Set<Entry<Integer, Integer>> entrySet = buyCar.entrySet();
			
			for (Entry<Integer, Integer> entry : entrySet) {
				//获得商品id
				Integer goodId = entry.getKey();
				//根据商品id获得商品
				Goods goods = goodsService.getShopById(goodId);
				//获得购物车中商品对应的购买数量
				Integer buyNum = buyCar.get(goodId);
				//封装到商品中
				goods.setBuyNum(buyNum);
				//添加商品到购物车显示列表
				goodsList.add(goods);
				//计算总价
				total +=goods.getPrice()*buyNum;
				
			}
			map.put("goodsList", goodsList);
		}


		
		map.put("total", total);
		
		return "shop_cart";
	}
}
